package com.ddwl.goods.service;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.GoodsEnum;
import com.ddwl.common.exception.ParamException;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.goods.dao.SignConfigDao;
import com.ddwl.goods.model.SignConfig;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.goods.SignConfigBo;
import com.ddwl.schema.bo.goods.SignConfigEnableBo;
import com.ddwl.schema.bo.goods.SignConfigPageBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.goods.SignConfigVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author hemiaojun
 * @Date 2020/10/19 15:39
 */
@Service
public class SignConfigService {

    @Resource
    private SignConfigDao signConfigDao;

    public SignConfigVo add(SignConfigBo bo) {
        ParamException.isNotNull(getByDay(bo.getDayOfWeek()), "请不要重复设置同一天的签到规则");
        SignConfig entity = BeanConverUtils.convertBean(bo, SignConfig.class);
        entity.setEnable(BaseEnum.StatusEnum.ENABLE.getCode());
        entity.setWeekCode(GoodsEnum.WeekEnum.getCodeBYDay(entity.getDayOfWeek()));
        signConfigDao.insert(entity);
        return BeanConverUtils.convertBean(entity, SignConfigVo.class);
    }

    public void update(SignConfigBo bo) {
        SignConfigVo vo = getByDay(bo.getDayOfWeek());
        if (null != vo) {
            ParamException.isFalse(vo.getId().equals(bo.getId()), "已经存在该日期的规则");
        }
        SignConfig entity = BeanConverUtils.convertBean(bo, SignConfig.class);
        signConfigDao.updateByPrimaryKeySelective(entity);
    }

    public void delete(String configId) {
        signConfigDao.deleteById(configId);
    }

    public SignConfigVo get(String configId) {
        return BeanConverUtils.convertBean(signConfigDao.selectByPrimaryKey(configId), SignConfigVo.class);
    }

    public PageVo<SignConfigVo> page(PageBo<SignConfigPageBo> bo) {
        PageHelper.startPage(bo.getPage(), bo.getPageSize(), MySqlUtil.orderBy(bo.getSort(), bo.isAsc()));
        PageInfo<SignConfig> info = new PageInfo(signConfigDao.getList(bo.getParam()));
        List<SignConfigVo> list = BeanConverUtils.convertBeanList(info.getList(), SignConfigVo.class);
        return new PageVo<>(info.getTotal(), bo.getPage(), bo.getPageSize(), list);
    }

    public void enableOrDisable(SignConfigEnableBo bo) {
        SignConfig entity = BeanConverUtils.convertBean(bo, SignConfig.class);
        signConfigDao.updateByPrimaryKeySelective(entity);
    }

    public SignConfigVo getByDay(String dayOfWeek) {
        SignConfig entity = new SignConfig();
        entity.setDayOfWeek(dayOfWeek);
        return BeanConverUtils.convertBean(signConfigDao.selectOne(entity), SignConfigVo.class);
    }

    public List<SignConfigVo> list() {
        PageBo<SignConfigPageBo> bo = new PageBo<>();
        bo.setSort("weekCode");
        bo.setAsc(true);
        return page(bo).getData();
    }
}
